#!/usr/bin/env python
# -*- coding: utf-8 -*-

import threading 
import xml.etree.cElementTree as ET

from loguru import logger

class AppConfig(object):
    _instance_lock = threading.Lock()
    def __init__(self):
        super().__init__()
        try:
            self.configtree = ET.parse("./data/config/configdata.xml")
        except:
            logger.exception("Error happens when parse config file: ./data/configdata.xml")
        else:
            logger.debug("Parse App config file successfully.")
        
    def __new__(cls, *args, **kwargs):
        if not hasattr(AppConfig, "_instance"):
            with AppConfig._instance_lock:
                if not hasattr(AppConfig, "_instance"):
                    AppConfig._instance = object.__new__(cls)

        return AppConfig._instance
    
    def __getElement(self, element_name):
        element = self.configtree.find(element_name)
        if element is  None:
            logger.warning("There is no {} node", element_name )
            return ""

        return element.text
    
    def getStyle(self):
        return self.__getElement("Style") + ".xml"
    
    def getMatlabSimulationMode(self):
        return self.__getElement("MatlabSimulationMode")

    def getACQCmdScriptFolder(self):
        return self.__getElement("ACQCmdScriptFolder")
    
    def getDriverControlExePath(self):
        return self.__getElement("DriverControlExePath")

    def getAcquisitionDataFolder(self):
        return self.__getElement("AcquisitionDataFolder")
    
    def getCropROIRegion(self):
        roi_string = self.__getElement("CropROIRegion")
        coordinate_list = roi_string.split(",")
        return coordinate_list
    
    def getReconstructionMethods(self):
        recon_methods = self.__getElement("ReconstructionMethods")
        recon_methods_list = recon_methods.split(",")
        return recon_methods_list

if __name__ == '__main__':
    config = AppConfig()
    style = config.getStyle()
    print(style)

    matlabSimMode = config.getMatlabSimulationMode()
    print(matlabSimMode)

    acqCmdFolder = config.getACQCmdScriptFolder()
    print(acqCmdFolder)

    driverControlExe = config.getDriverControlExePath()
    print(driverControlExe)

    acquisitionDataFolder = config.getAcquisitionDataFolder()
    print(acquisitionDataFolder)

    cropROIRegion = config.getCropROIRegion()
    print(cropROIRegion)
